<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../react18/react.development.js"></script>
    <script src="../react18/react-dom.development.js"></script>
    <script src="../react18/babel.min.js"></script>
</head>

<body>
    <div id="app"></div>
    <script type="text/babel">
        /*
        props是用来做父子组价通信的
          -在父组件中使用子组件时,可以给子组件添加属性
          -在子组件实例的props中可以拿到父组件添加的属性
        
        */

        //创建父组件
        class Parent extends React.Component{
            state = {
                car: "八手奥拓"
            }
            render(){
                //解构数据
                const {car} = this.state
                return (
                    <div>
                       <h1 onClick = {this.changeCarHandle}>我是父组件</h1> 
                       <p>我开的车是{car}</p>
                       <hr />
                       <Child a={1} car={car} />  
                    </div>
                )
            }

            changeCarHandle = () => {
                this.setState({car:"奥迪RS7"})
            }
        }

        //创建子组件
        class Child extends React.Component{
            
            render(){
                console.log("子组件实例",this);//打印出来的props是父组件传过来的元素组成的对象
                const {car} = this.props
                return (
                    <div>
                       <h1>我是子组件</h1> 
                       <p>我开我爹的车{car}</p>
                       <hr />
                    </div>
                )
            }
        }

        //创建容器
        const root = ReactDOM.createRoot(document.getElementById("app"))
        root.render(<Parent />)
    </script>
</body>

</html>